const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");

module.exports = {
  optimization: {
    minimize: false, // 关闭代码压缩，可选
  },
  //指定项目的入口文件
  entry: "./src/index.ts",

  devtool: "inline-source-map",

  // devServer: {
  //   contentBase: "./dist",
  // },
  //指定打包文件所在目录
  output: {
    path: path.resolve(__dirname, "dist"),
    //打包后文件的名字
    filename: "bundle.js",
    environment: {
      arrowFunction: false, // 关闭webpack的箭头函数，可选
    },
  },

  //用来设置引用的模块
  resolve: {
    //表示以ts,js结尾的文件都可以作为模块使用
    //没有这个配置，ts文件中写import和export模块语法时候,打包会报错
    extensions: [".ts", ".js"],
  },
  mode: "development",
  //指定webpack打包时候用到的loader
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: [
          {
            loader: "babel-loader",
            options: {
              presets: [
                [
                  "@babel/preset-env",
                  {
                    targets: {
                      chrome: "58",
                      ie: "11",
                    },
                    corejs: "3",
                    useBuiltIns: "usage",
                  },
                ],
              ],
            },
          },
          {
            loader: "ts-loader",
          },
        ],
        exclude: /node_modules/,
      },
      {
        test: /\.less$/,
        use: [
          "style-loader",
          "css-loader",
          //引入postcss
          {
            loader: "postcss-loader",
            options: {
              postcssOptions: {
                plugins: [
                  [
                    "postcss-preset-env",
                    {
                      //兼容每种浏览器的最新两个版本
                      browsers: "last 2 versions",
                    },
                  ],
                ],
              },
            },
          },
          "less-loader",
        ],
      },
    ],
  },
  //配置webpack插件
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      template: "./public/index.html",
    }),
  ],
};
